package com.zhao.dota.controller;

import com.zhao.dota.model.Round;
import com.zhao.dota.service.QuotationService;
import com.zhao.dota.service.RoundService;
import com.zhao.dota.struct.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/{leagueId}/round")
public class RoundController {

    @Resource
    private RoundService roundService;

    @GetMapping("/")
    public Result<List<Round>> list(@PathVariable Integer leagueId) {
        List<Round> list = roundService.leagueRound(leagueId);
        return Result.success(list);
    }

    @GetMapping("/quoting")
    public Result<Map<String, Object>> quoting(@PathVariable Integer leagueId) {
        Round quoting = roundService.quoting(leagueId);
        Map<String, Object> result = new HashMap<>();
        result.put("types", Round.Type.values());
        result.put("quoting", quoting);
        return Result.success(result);
    }

    @PostMapping("/")
    public Result<Round> save(@PathVariable Integer leagueId, Round round) {
        round = roundService.save(leagueId, round);
        return Result.success(round);
    }

    @GetMapping("/endQuotation")
    public Result<Boolean> endQuotation(@PathVariable Integer leagueId) {
        boolean end = roundService.endQuotation(leagueId);
        return Result.success(end);
    }

    @DeleteMapping("/cancelRoll")
    public Result<Void> cancelRoll(@PathVariable Integer leagueId, Integer roundId) {
        boolean b = roundService.cancelRoll(roundId);
        return Result.complete(b);
    }

    @GetMapping("/endRoll")
    public Result<Void> endRoll(@PathVariable Integer leagueId, Integer roundId) {
        roundService.endRoll(roundId);
        return Result.success();
    }

    @PostMapping("/start")
    public Result<Round> start(@PathVariable Integer leagueId, Round.Type type) {
        Round start = roundService.start(leagueId, type);
        return Result.success(start);
    }

}
